<template>
  <div class="info_config_page">
    <div class="flex align-center table-top-btns">
      <h5 class="flex align-center">
        <svg-icon
          style="margin-right:15px"
          icon-class="list"
          :size="15"
        />数据列表
      </h5>
      <el-button
        size="small"
        class="add-btn"
        icon="el-icon-plus"
        v-if="$hasMethod('#add')"
        @click="$router.push({name:'articleInfoConfigAdd'})"
      >新添信息</el-button>
    </div>
    <el-table
      :data="tableData"
      row-key="id"
      border
      :tree-props="{children: 'children'}"
    >
      <el-table-column
        align="center"
        label="展开"
        width="80"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="排序"
        width="80"
      >
        <template slot-scope="scope">
          <!-- onkeyup="value=value.replace(/[^\d]/g,'')" -->
          <span v-if="!$hasMethod('#sort')">{{scope.row.sort}}</span>
          <el-input
            v-else
            oninput="value=value.replace(/[^\d]/g,'')"
            maxLength='9'
            v-model="scope.row.sort"
            @change="changeSort(scope.row.id, scope.row.sort)"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="id"
        label="ID"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="分类名称"
      >
        <template slot-scope="scope">
          <p><span
              style="margin:0 5px 0 20px;"
              v-if="!scope.row.children"
            >├─</span>{{scope.row.type_name}}</p>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        width="180"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="$hasMethod('#enable')"
            class="pointer"
            :type="scope.row.status==1?'success':'danger'"
            @click="changeStatus(scope.row.id, scope.row.status)"
          >{{scope.row.status==1?'显示':'隐藏'}}</el-tag>
          <el-tag
            v-else
            :type="scope.row.status==1?'success':'danger'"
          >{{scope.row.status==1?'显示':'隐藏'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        width="280"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.children && $hasMethod('#add')"
            size="mini"
            type="success"
            @click="$router.push({name:'articleInfoConfigAdd',params:{parentId:scope.row.id}})"
          >添加子菜单</el-button>
          <el-button
            size="mini"
            type="primary"
            v-if="$hasMethod('#edit')"
            @click="$router.push({name:'articleInfoConfigAdd',params:{parentId:scope.row.parentid, editId:scope.row.id}})"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            v-if="$hasMethod('#delete')"
            @click="removeItem(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getInfoTypeList, addOrEditInfoConfig, delInfoConfig, editArticleTypeStatus } from "@/api/web";
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    // 列表项删除
    removeItem(id) {
      this.$confirm('您确定要删除吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        delInfoConfig(id).then(res => {
          this.getList();
        });
      }).catch(() => { });
    },
    // 更改显示隐藏状态
    changeStatus(id, status) {
      let value = status == 1 ? 2 : 1;
      editArticleTypeStatus({
        id: id,
        status: value
      }).then(res => {
        this.getList();
      })
    },
    // 更改排序
    changeSort(id, val) {
      addOrEditInfoConfig({
        id: id,
        sort: val
      }).then(res => {
        this.getList();
      })
    },
    // 获取列表
    getList() {
      getInfoTypeList().then(res => {
        // console.log(res);
        this.tableData = res;
      })
    }
  },
  mounted() {
    this.getList();
  }
}
</script>

<style lang="scss" scoped>
</style>
